﻿using Orchard;
using Orchard.DisplayManagement;
using Orchard.Localization.Services;
using Orchard.Mvc;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NineSeas.Dashboard.Controllers
{
    public class LocalizeController : Controller
    {
        private readonly IShapeFactory _shapeFactory;
        private readonly IOrchardServices _services;
        private readonly ICultureManager _cultureManager;
        public LocalizeController(IOrchardServices services, IShapeFactory shapeFactory, ICultureManager cultureManager)
        {
            _shapeFactory = shapeFactory;
            _services = services;
            _cultureManager = cultureManager;
        }
        //
        // GET: /Localize/

        public ActionResult Index(string name)
        {
            var shapeName = "Localize_" + name;
            dynamic shape = _shapeFactory.Create(shapeName);
            Response.AddHeader("Content-Type", "application/json");
            return new ShapeResult(this, shape);
        }

        public ActionResult ChangeCulture(string culture)
        {
            HttpContextBase httpContext = _services.WorkContext.HttpContext;
            HttpRequestBase request = httpContext.Request;

            var cultureCookie = new HttpCookie("cultureData");
            cultureCookie.Values.Add("currentCulture", culture);
            cultureCookie.Expires = DateTime.Now.AddYears(1);

            //setting up domain for cookie allows to share it to sub-domains as well
            //if non-default port is used, we consider it as a testing environment without sub-domains
            if (request.Url != null && request.Url.IsDefaultPort)
            {
                // '.' prefix means, that cookie will be shared to sub-domains
                cultureCookie.Domain = "." + request.Url.Host;
            }

            httpContext.Response.Cookies.Add(cultureCookie);
            return Json(new {
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetCultureSettings()
        {
            var availableCultures = _cultureManager.ListCultures();
            var availableCultureswithNames = new List<KeyValuePair<string, string>>();
            foreach (string culture in availableCultures)
            {
                try
                {
                    var cultureInfo = new CultureInfo(culture);
                    availableCultureswithNames.Add(new KeyValuePair<string, string>(culture, cultureInfo.NativeName));
                }
                catch
                {
                    availableCultureswithNames.Add(new KeyValuePair<string, string>(culture, culture));
                }
            }
            //sort cultures by native name
            availableCultureswithNames.Sort((k1, k2) => String.CompareOrdinal(k1.Value, k2.Value));
            return Json(new {
                currentCulture = _cultureManager.GetSiteCulture(),
                availableCultures = availableCultureswithNames
            }, JsonRequestBehavior.AllowGet);
        }

    }
}
